package com.EE7th.service.impl;


import com.EE7th.Result.PageResult;
import com.EE7th.dao.ClazzDao;
import com.EE7th.domain.Clazz;
import com.EE7th.dto.*;
import com.EE7th.service.ClazzService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzDao clazzDao;

    @Override
    @Transactional
    public Boolean insertClazz(Clazz clazz) {
        return clazzDao.insertClazz(clazz) > 0;
    }

    @Override
    public Clazz selectById(int clazzId) {
        return clazzDao.selectById(clazzId);
    }

    @Override
    public PageResult pageQuery(PageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());

        List<Clazz> clazzList = clazzDao.selectAll();
        List<ClazzDTO> clazzDTOList = new ArrayList<>();
        for (Clazz clazz : clazzList) {
            TeacherDTO teacherDTO = clazzDao.getTeacherNameByClazzId(clazz.getClazzId());
            if (teacherDTO != null) {
                clazzDTOList.add(new ClazzDTO(clazz, teacherDTO));
            } else {
                clazzDTOList.add(new ClazzDTO(clazz, null, null));
            }
        }

        PageInfo<ClazzDTO> pageInfo = new PageInfo<>(clazzDTOList);
        return new PageResult(clazzDao.selectAll().size(), pageInfo.getList());
    }

    @Override
    public List<Clazz> getClazzSelection() {
        return clazzDao.getClazzSelection();
    }
}